home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: while loop problem
- Date: Sat, 16 Mar 96 20:36:09 GMT
- Organization: none
- Message-ID: <827008569snz@genesis.demon.co.uk>
- References: <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca> <Do6DB0.EtE@microunity.com> <Pine.A32.3.91.960312171258.149477C-100000@black.weeg.uiowa.edu> <DoBAC8.K3o@iquest.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <DoBAC8.K3o@iquest.net> dlmiller@iquest.net "Doug Miller" writes:
-
- >The Amorphous Mass <robinson@blue.weeg.uiowa.edu> wrote:
- >
- >+On Tue, 12 Mar 1996, Tom Sanders wrote:
- >+
- >+> Bill Simpson <wsimpson@uwinnipeg.ca> writes:
- >+> |> Consider the following code fragment:
- >+> |>
- >+> |> y=2000; z=1000;
- >+> |> x=0;
- >+> |> while(x<=XMAX)
- >+> |> {
- >+> |> x+=(int) erand(mean);
- >+> |> setdot(x,y,z);
- >+> |> }
- >+> |>
- >+> |> The above code will also attempt to plot one point at an x value >XMAX
- >+> |> before it terminates.
- >+> |>
- >+> |> Is there a nice way to write the code so it works properly?
- >+>
- >+> y=2000; z=1000;
- >+> x=0;
- >+> do {
- >+> x+=(int) erand(mean);
- >+> setdot(x,y,z);
- >+> } while(x<=XMAX)
- >+
- >+ This has exactly the same problem as the original loop, because it adds
- >+erand() to x and calls setdot() _before_ checking x's value against XMAX.
- >+Also, you left off the semicolon after the while ().
- >+
- >+/**James Robinson***********************
- >
- >Well, let's give him a correct answer.
- >
- >In the *original* loop, just change the test (x <= XMAX) to (x < XMAX).
- >There.
-
- How does that make it many more correct? I see nothing in the problem
- description to suggest that XMAX is not a valid value for x in a call to
- setdot() - it appears that it is valid. Even if you make that change to
- the original it doesn't cure the problem when erand() returns a number
- greater than 1 (I see nothing to suggest it can't).
-
- > That wasn't so hard, was it?
-
- Apparently so.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-